//
//  HLIMKitTitleView.m
//  YueYue
//
//  Created by hulu001 on 2024/1/11.
//

#import "HLIMKitTitleView.h"
#import "HLUserDetailViewController.h"
#import "HLUserOptionRequest.h"

@implementation HLIMKitTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-108, 40));
        }];
        
        self.avatarView = [[QMUIButton alloc] init];
        _avatarView.layer.masksToBounds = YES;
        _avatarView.layer.cornerRadius = 12;
        [_avatarView addTarget:self action:@selector(userDetail) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(36);
            make.left.centerY.mas_equalTo(0);
        }];
        
        
        self.titleLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(15) textColor:HLTextColor1];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_avatarView);
            make.left.equalTo(_avatarView.mas_right).offset(8);
        }];
        
        self.detailLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(12) textColor:HLTextColor3];
        _detailLabel.hidden = YES;
        [self addSubview:_detailLabel];
        [_detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLabel);
            make.bottom.mas_equalTo(_avatarView);
        }];

        self.followButton = [[QMUIButton alloc] init];
        _followButton.cornerRadius = 12;
        _followButton.hidden = YES;
        _followButton.titleLabel.font = UIFontMake(12);
        _followButton.contentEdgeInsets = UIEdgeInsetsMake(6, 12, 6, 12);
        [_followButton addTarget:self action:@selector(followAction:) forControlEvents:UIControlEventTouchUpInside];
       [self addSubview:_followButton];
        [_followButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.centerY.mas_equalTo(0);
        }];
    }
    return self;
}

- (void)setModel:(HLUserInfoModel *)model
{
    _model = model;
    if (model.lastLoginTime){
        
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_avatarView);
            make.left.equalTo(_avatarView.mas_right).offset(8);
        }];
        _detailLabel.hidden = NO;
        _detailLabel.text = [HLHelper showTime:[HLHelper currentTimeStamp] showDetail:YES];
    }
    
    _followButton.hidden = NO;
    [self refreshFollowType:model.followType];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:HLUserFollowDidChangeNotification object:nil];
}

- (void)refreshFollowType:(NSString *)followType
{
    self.followButton.hidden = NO;
    if ([followType isEqualToString:@"0"]) {
        // 仅关注
        [self.followButton setTitle:@"已关注" forState:UIControlStateNormal];
        [self.followButton setTitleColor:HLMainColor forState:UIControlStateNormal];
        self.followButton.layer.borderWidth = 0.5;
        self.followButton.layer.borderColor = HLMainColor.CGColor;
    } else if ([followType isEqualToString:@"1"]) {
        // 已互关
        [self.followButton setTitle:@"互相关注" forState:UIControlStateNormal];
        [self.followButton setTitleColor:HLMainColor forState:UIControlStateNormal];
        self.followButton.layer.borderWidth = 0.5;
        self.followButton.layer.borderColor = HLMainColor.CGColor;
        self.followButton.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    } else if ([followType isEqualToString:@"2"]) {
        // 已拉黑
        self.followButton.hidden = YES;
    } else {
        [self.followButton setTitle:@"关注" forState:UIControlStateNormal];
        [self.followButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        self.followButton.layer.borderWidth = 0;
        self.followButton.backgroundColor = HLMainColor;
    }
}

- (void)notification:(NSNotification *)noti
{
    NSDictionary *info = noti.object;
    NSInteger type = [[info objectForKey:@"type"] intValue];
    NSString *user = [info objectForKey:@"user"];
    
    if (![user isEqualToString:_model.userId]){
        return;
    }
    
    if (type == HLUserOptionTypeFollow){
        _model.followType = @"0";
    } else {
        _model.followType = @"1";
    }
    [self refreshFollowType:_model.followType];
}


- (void)followAction:(UIButton *)button
{
    
    BOOL hasFollow = [self.model.followType isEqualToString:@"0"];
    MJWeakSelf
    [HLUIHelper followWithUserId:self.model.userId follow:hasFollow callback:^(BOOL isConfirm) {
        if(isConfirm){
            NSString *type = hasFollow ? @"1" : @"0";
            weakSelf.model.followType = type;
            [weakSelf refreshFollowType:type];
        }
    }];
}

- (void)userDetail
{
    if (_model){
        HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:_model.userId];
        [[QMUIHelper visibleViewController].navigationController pushViewController:userDetailVC animated:YES];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
